#include "queue.h"


Queue::Queue(int qs): qsize(qs), front(nullptr), rear(nullptr), nitem(0) {}
Queue::~Queue()
{
    // 删除剩余的所有节点
    Node * temp;
    while(front != nullptr)
    {
        temp = front;
        front = front->next;
        delete temp;
    }
    
}

bool Queue::isempty() const
{
    return nitem == 0;
}
bool Queue::isfull() const
{
    return nitem == qsize;
}
int Queue::queuecount() const
{
    return nitem;
}
bool Queue::enqueue(const Item &item)
{
    // 满了
    if (isfull())
        return false;

    // 没满，分配内存   
    Node * temp = new Node;
    temp->item = item;
    temp->next = nullptr;
    ++nitem;

    if(front == nullptr) // 除非栈空，否则入栈只动栈尾
        front = temp;
    else
        rear->next = temp;
    rear = temp;
    return true;
}
bool Queue::dequeue(Item &item)
{
    if (isempty()) return false;

    Node* temp = front;
    front = front->next;
    delete temp;

    --nitem;
    if (nitem == 0) // 除非栈空，否则出栈只动栈首
        rear = nullptr; // !

    return true;
}